// request.js
// 创建新的axios实例
import axios from 'axios';
import {message} from "ant-design-vue";

const service = axios.create({
    // 环境变量，需要在.env文件中配置
    baseURL: "http://127.0.0.1:8012/api/",
    // 超时时间暂定5s
    timeout: 5000,
    headers: {
        'Content-Type': 'application/json;charset=UTF-8'
    }
});
// 请求拦截器
service.interceptors.request.use((config) => {
    config.headers['token'] = localStorage.getItem('token');
    return config;
});

// 响应拦截器
service.interceptors.response.use((response => {
    const res = response.data;
    if (res.code !== 200) {
        return Promise.reject(new Error(res.message || '网络异常'));
    } else {
        return res;
    }
}), err => {
    message.error(err.message);
    return Promise.reject(err);
});


//封装get 请求
export const get = (url, params) => {
    return service({
        url,
        method: 'get',
        params
    })
}

//封装post 请求
export const post = (url, data) => {
    return service({
        url,
        method: 'post',
        data
    })
}

